//
//  NMyFocusViewController.m
//  News
//
//  Created by zhangtao on 2018/10/13.
//  Copyright © 2018年 zt.td. All rights reserved.
//

#import "NMyFocusViewController.h"
#import "NWalletDetailHeaderView.h"
#import "NMyFocusCell.h"
#import "NMyFocusThemeTableViewCell.h"
#import "FollowTopicOfSubscriberModel.h"

@interface NMyFocusViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)NWalletDetailHeaderView *topView;
@property(nonatomic,strong)UITableView *table;
@property (nonatomic, assign) NSInteger pageIndex;
@property (nonatomic, strong) NSMutableArray *dataArray;

@property(nonatomic,assign)BOOL foucsPerson; //关注的主题/人
@end

@implementation NMyFocusViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的关注";
    self.view.backgroundColor = [UIColor whiteColor];
    [self createUI];
    
    [self addRefreshFunction];
    [self addLoadMoreFunction];
    
}

-(void)createUI{
    _topView = [[NWalletDetailHeaderView alloc]initWithFrame:CGRectMake(0, navHeight, ACEWidth, 40) items:@[@"关注的主题"]];
    WEAKSELF;
    _topView.WalletDetailViewCallBack = ^(NSInteger tag) {
        if (tag == 0) {
            weakSelf.foucsPerson = NO;
        }else{
            weakSelf.foucsPerson = YES;
        }
        [weakSelf.table reloadData];
    };
    [self.view addSubview:_topView];
    _table = [[UITableView alloc]initWithFrame:CGRectMake(0, navHeight+40, ACEWidth, ACEHeight-navHeight-40) style:UITableViewStyleGrouped];
    _table.delegate = self;
    _table.dataSource = self;
    _table.separatorColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:_table];
    [_table registerNib:[UINib nibWithNibName:@"NMyFocusCell" bundle:nil] forCellReuseIdentifier:@"personCell"];
    [_table registerNib:[UINib nibWithNibName:@"NMyFocusThemeTableViewCell" bundle:nil] forCellReuseIdentifier:@"themeCell"];

    
}

#pragma mark AddTableViewPlugin
- (void)addRefreshFunction
{
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader
                                     headerWithRefreshingTarget:self
                                     refreshingAction:@selector(refreshAction)];
    // 隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES;
    // 马上进入刷新状态
    [header beginRefreshing];
    // 设置header
    self.table.mj_header = header;
}

- (void)addLoadMoreFunction
{
    MJRefreshAutoStateFooter *footer = [MJRefreshAutoStateFooter
                                        footerWithRefreshingTarget:self
                                        refreshingAction:@selector(loadMoreAction)];
    // 设置footer
    self.table.mj_footer = footer;
    self.table.mj_footer.automaticallyHidden = YES;
}
#pragma mark RefreshAction
- (void)refreshAction
{
    self.pageIndex = 1;
    [self getData:1];
}

- (void)loadMoreAction
{
    self.pageIndex ++;
    [self getData:self.pageIndex];
}

- (void)getData:(NSInteger)pageIndex
{
    [[NHTTPManage shareInstance]afGet:HTTP_GET_TOPICAL_GetFollowTopicOfSubscriber andParam:@{@"cuid":CUID,@"cp":@(pageIndex)} success:^(id  _Nonnull responseObject) {
        //结束头部刷新
        [self.table.mj_header endRefreshing];
        //结束尾部刷新
        [self.table.mj_footer endRefreshing];
        if (self.dataArray.count > 0 && self.pageIndex == 1) {
            [self.dataArray removeAllObjects];
        }
        [responseObject[@"Data"] enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            FollowTopicOfSubscriberModel *model = [FollowTopicOfSubscriberModel modelObjectWithDictionary:obj];
            [self.dataArray addObject:model];
        }];
        if ([responseObject[@"Data"] count] == 0) {
            [self.table.mj_footer endRefreshingWithNoMoreData];
        }
        [self.table reloadData];
    } failture:^(id  _Nonnull error) {
        //结束头部刷新
        [self.table.mj_header endRefreshing];
        //结束尾部刷新
        [self.table.mj_footer endRefreshing];
        NSLog(@"%@",error);
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return _foucsPerson? 45 : 55;;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return .1;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (_foucsPerson) {
        NMyFocusCell *cell = [tableView dequeueReusableCellWithIdentifier:@"personCell"];
        return cell;
    }else{
        NMyFocusThemeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"themeCell"];
        [cell bindData:self.dataArray[indexPath.row]];
        return cell;

    }
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end
